package com.xii.eduservice.controller;


import com.xii.commonutils.R;
import com.xii.eduservice.client.VodClient;
import com.xii.eduservice.entity.EduVideo;
import com.xii.eduservice.service.EduVideoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author xhjava
 * @since 2022-02-13
 */
@RestController
@Api(description = "小节管理")
//@CrossOrigin
@RequestMapping("/eduservice/eduvideo")
public class EduVideoController {
    @Autowired
    private EduVideoService eduVideoService;

    @Autowired
    private VodClient vodClient;


    /**
     * @description: 根据id查询小节
     *
     * @author  wangxihao
     * @email wangxh0108@163.com
    **/
    @ApiOperation(value = "根据id查询")
    @GetMapping("getVideoInfoById/{videoId}")
    public R getChapter(@PathVariable String videoId){
        EduVideo byId = eduVideoService.getById(videoId);
        return R.ok().data("video",byId);
    }

    /**
     * @description: 添加小节
     * 
     * @author  wangxihao
     * @email wangxh0108@163.com
    **/
    @ApiOperation(value = "添加小节")
    @PostMapping("addVideo")
    public R addVideo(@RequestBody EduVideo eduVideo){
        eduVideoService.save(eduVideo);
        return R.ok();
    }

    /**
     * @description: 删除小节
     *
     * @author  wangxihao
     * @email wangxh0108@163.com
    **/
//    TODO  待完善 课时部分       ---已完善
    @ApiOperation(value = "删除小节")
    @DeleteMapping("deleteVideo/{videoid}")
    public R deleteById(@PathVariable String videoid){

        //删除小节中的视频 根据视频id
        //先得到视频id

        EduVideo byId = eduVideoService.getById(videoid);
        String videoSourceId = byId.getVideoSourceId();

        if(!StringUtils.isEmpty(videoSourceId)){
            //远程调用  删除视频
            vodClient.removeVideo(videoSourceId);
        }
        //删除小节
        eduVideoService.removeById(videoid);
        return R.ok();

    }

    /**
     * @description: 修改小节
     *
     * @author  wangxihao
     * @email wangxh0108@163.com
    **/
    @ApiOperation(value = "修改小节")
    @PostMapping("updateVideo")
    public R updateVideo(@RequestBody EduVideo eduVideo){
        eduVideoService.updateById(eduVideo);
        return R.ok();
    }
}

